package org.atf.model.entity;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table
@NamedQueries({
	 @NamedQuery(name="Server.findAllPreference",
            	query="select s.preference from Server s where s.id = :serverId")
	
})
public class Server implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1317798702076522572L;
	/**
	 * 
	 */

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "SERVER_ID")
	private Integer id;
	private String servername;
	private String hostname;
	private String nodename;
	private String status;
	private String cellName;
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(
	        name = "SERVER_PREFERENCE",
	        joinColumns = @JoinColumn(name = "SERVER_ID"),
	        inverseJoinColumns = @JoinColumn(name = "PREFERENCE_ID")
	)
	private Set<Preference> preference= new HashSet<Preference>();
	public Server(){
		
	}

	public Server(String sName, String hName, String nName){
		this.servername=sName;
		this.hostname=hName;
		this.nodename=nName;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getServername() {
		return servername;
	}
	public void setServername(String servername) {
		this.servername = servername;
	}
	public String getHostname() {
		return hostname;
	}
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}
	public String getNodename() {
		return nodename;
	}
	public void setNodename(String nodename) {
		this.nodename = nodename;
	}
	
	
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Set<Preference> getPreference() {
		return preference;
	}
	public void setPreference(Set<Preference> preference) {
		this.preference = preference;
	}
	public void addPreference(Preference pref){
		if(!preference.contains(pref)){
			preference.add(pref);
		}
	}
	public void removePreference(Preference pref){
		if(preference.contains(pref)){
			preference.remove(pref);
		}else{
			System.out.println("unable to remove");
		}
	}
	
	public String getCellName() {
		return cellName;
	}

	public void setCellName(String cellName) {
		this.cellName = cellName;
	}

	@Override
	public String toString() {
		return "Server [id=" + id + ", servername=" + servername
				+ ", hostname=" + hostname + ", nodename=" + nodename
				+ ", status=" + status + "]";
	}

	public void updateProperties(Server server) {
		// TODO Auto-generated method stub
		this.servername=server.getServername();
		this.hostname=server.getHostname();
		this.nodename=server.getNodename();
		this.status=server.getStatus();
		this.cellName=server.getCellName();
		if(server.getPreference() != null && !server.getPreference().isEmpty()){
			this.preference=server.getPreference();
		}
	}
	
	
	
	
}
